/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.gui;

import static java.awt.GridBagConstraints.BOTH;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.concurrent.Executor;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;

import edu.strath.cis.grallan.findbugs.adaptiveranking.AdaptiveRankedBugList;
import edu.strath.cis.grallan.findbugs.adaptiveranking.AdaptiveRanker;
import edu.strath.cis.grallan.findbugs.adaptiveranking.lr.LearningRank;
import edu.umd.cs.findbugs.gui2.ApplicationDisplayIssueDecider;
import edu.umd.cs.findbugs.gui2.BugSetFactory;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class LearningMode extends AbstractSwingRankingMode<LearningRank> {

	private static final int MAX_NUM_LEARNING_BUGS_TO_SHOW = 1000;
	private static final String LABEL = "Learning Mode";
	private static final String NAME = "LearningMode";

	private static final String COMPONENT_NAME = "LearningModePanel";
	private static final String UNCALCULATED = "UncalculatedCard";
	private static final String LEARNING = "LearningCard";
	private static final String HELP_CARD = "HelpCard";

	private final AdaptiveRanker<LearningRank> learningValueRanker;

	private JPanel cardPanel;

	public LearningMode(AdaptiveRanker<LearningRank> learningValueRanker, RankedBugTree tree,
			IRankingViewController viewController, Executor calculationExecutor, BugSetFactory bugSetFactory,
			ApplicationDisplayIssueDecider applicationDisplayDeciderPrototype) {
		super(NAME, LABEL, new LearningHelpInfo(), learningValueRanker, tree, viewController, bugSetFactory,
				calculationExecutor, applicationDisplayDeciderPrototype);
		this.learningValueRanker = learningValueRanker;

		initialiseComponents();

		this.learningValueRanker.addStateListener(this);
	}

	@Override
	protected JPanel makeContainerForMode() {
		return new JPanel();
	}

	private void initialiseComponents() {
		containerForMode.setName(COMPONENT_NAME);
		containerForMode.setLayout(new BorderLayout());

		JPanel learningPanel = new JPanel(new BorderLayout());
		cardPanel = new JPanel(new CardLayout());

		JPanel calculatedPanel = new JPanel(new BorderLayout());

		JToolBar calculatedToolbar = new JToolBar(JToolBar.HORIZONTAL);
		calculatedToolbar.setFloatable(false);

		calculatedToolbar.add(makeCalculateButton());
		calculatedToolbar.add(makeRefreshButton());
		calculatedToolbar.add(makeToggleFilterButton());
		calculatedToolbar.add(makeAutoRefreshButton());
		calculatedToolbar.add(makeHelpButton());

		learningPanel.add(calculatedToolbar, BorderLayout.NORTH);

		calculatedPanel.add(makeTreePane(), BorderLayout.CENTER);

		cardPanel.add(calculatedPanel, LEARNING);

		JScrollPane helpPane = new JScrollPane(makeHelpTextComponent());
		cardPanel.add(helpPane, HELP_CARD);

		cardPanel.add(makeUncalculatedPanel(), UNCALCULATED);

		learningPanel.add(cardPanel, BorderLayout.CENTER);
		containerForMode.add(learningPanel, BorderLayout.CENTER);

		setStates();
	}

	private JPanel makeUncalculatedPanel() {
		JPanel uncalculatedPanel = new JPanel(new GridBagLayout());
		GridBagConstraints con = new GridBagConstraints();
		con.insets = new Insets(6, 6, 6, 6);
		con.gridx = con.gridy = 0;
		con.weightx = 1;
		con.fill = BOTH;

		JPanel space = new JPanel();
		con.gridx = 0;
		con.gridy = 1;
		con.fill = BOTH;
		con.weighty = 1;
		uncalculatedPanel.add(space, con);

		return uncalculatedPanel;
	}

	private void setStates() {
		setActionStates();
		setCurrentCard();
	}

	private void setActionStates() {
		refreshAction.setEnabled(learningValueRanker.canRefresh());
		boolean rankingComputed = learningValueRanker.rankingComputed();
		toggleFilterAction.setEnabled(rankingComputed);
		toggleAutoRefreshAction.setEnabled(rankingComputed);
		calculateAction.setEnabled(learningValueRanker.canCompute() && !rankingComputed);
	}

	protected void showLearningBugsTree() {
		showCard(LEARNING);
	}

	public void select() {
		setStates();
		doClickSelectedBug();
	}

	private void setCurrentCard() {
		if (learningValueRanker.rankingComputed() && !helpAction.helpSelected()) {
			showCard(LEARNING);
		} else if (helpAction.helpSelected()) {
			showCard(HELP_CARD);
		} else {
			showCard(UNCALCULATED);
		}
	}

	private void showCard(String cardToShow) {
		CardLayout cards = (CardLayout) cardPanel.getLayout();
		cards.show(cardPanel, cardToShow);
	}

	public void notifyBugCollectionSet() {
		setStates();
	}

	public void notifyReset() {
		setStates();
	}

	public void notifyCanRefresh() {
		super.notifyCanRefresh();
		setStates();
	}

	public void notifyRefreshComplete() {
		doClickSelectedBug();
	}

	public void notifyRankingComputed() {
		setRankingOrder();
		setStates();
		doClickSelectedBug();
	}

	public void notifyRankingStarted(int numberOfBugs) {
		setStates();
	}

	public void notifySingleRankComputed() {
		setStates();

	}

	@Override
	protected void doRefresh() {
		learningValueRanker.refresh();
	}

	@Override
	protected void doCalculate() {
		learningValueRanker.computeRankOrder();
	}

	@Override
	protected void doHelpAction() {
		setCurrentCard();
	}

	@Override
	protected AdaptiveRankedBugList<LearningRank> getRankedList() {
		AdaptiveRankedBugList<LearningRank> rankedList = super.getRankedList();
		AdaptiveRankedBugList<LearningRank> topRanked = new AdaptiveRankedBugList<LearningRank>();

		int maxBugs = getNumBugsToShow(rankedList);
		for (LearningRank bug : rankedList.subList(0, maxBugs)) {
			topRanked.add(bug);
		}
		return topRanked;
	}

	private int getNumBugsToShow(AdaptiveRankedBugList<LearningRank> rankedList) {
		int maxBugs = rankedList.size() > MAX_NUM_LEARNING_BUGS_TO_SHOW ? MAX_NUM_LEARNING_BUGS_TO_SHOW : rankedList
				.size();
		return maxBugs;
	}
}
